package ru.kantiana.accounting.rldata.core.rl.path;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * @author: Pavel Belevich
 * Date: 3/27/11
 * Time: 1:00 AM
 */
public class PathBuilderImpl implements PathBuilder {

    private String currentYearRoot;
    private String archiveRoot;
    private String monthDir;
    private String yearDir;

    private static NumberFormat twoCharsFormat = new DecimalFormat("00");

    public PathBuilderImpl(String currentYearRoot, String archiveRoot, String monthDir, String yearDir) {
        this.currentYearRoot = currentYearRoot;
        this.archiveRoot = archiveRoot;
        this.monthDir = monthDir;
        this.yearDir = yearDir;
    }

    public String getPathFor(int year, int month) {
        Calendar calendar = new GregorianCalendar();
        int currentMonth = calendar.get(Calendar.MONTH);
        int currentYear = calendar.get(Calendar.YEAR);
        if (currentYear == year) {
            if (currentMonth == month) {
                return currentYearRoot + "\\" + monthDir;
            } else {
                return currentYearRoot + "\\" + yearDir + "." + twoCharsFormat.format(year % 100) + "\\" + monthDir + "." + twoCharsFormat.format(month + 1);
            }
        } else {
            return archiveRoot + "\\" + yearDir + "." + twoCharsFormat.format(year % 100) + "\\" + monthDir + "." + twoCharsFormat.format(month + 1);
        }
    }
}
